﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Admin
{
	public partial class Admin_CourseAdd : System.Web.UI.Page
	{
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[4].Selected = true;
            Master.FindControl("ContentPlaceholder1").FindControl("Button2").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button3").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button4").Visible = false;

        }

        protected void Add_Click(object sender, EventArgs e)
        {
            /*Get info from textboxes*/

            string id = ID.Text;
            string name = Name.Text;

            string currentSemester = null;
            string currentYear = DateTime.Now.Year.ToString();
            int month = Convert.ToInt32(DateTime.Now.Month.ToString());

            currentSemester = null;

            /*Dynamic Semester Parser ::G*/
            if (month >= 1 && month <= 5)
            {
                currentSemester = "SPRING";
            }
            else if (month >= 6 && month <= 9)
            {
                currentSemester = "SUMMER";
            }
            else if (month >= 9 && month <= 12)
            {
                currentSemester = "FALL";
            }


            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string insertStaff = "INSERT "
                                 + "INTO Course "
                                 + "VALUES ( '" + id+ "', "  +"'" + name + "'"+ ")";

            string query2 = "SELECT S.semId " +
                "FROM Semester S " +
                "WHERE S.year='" + currentYear + "' AND S.season='" + currentSemester + "'";

            sqlCommand = new MySqlCommand(query2, conn);
            readVals = sqlCommand.ExecuteReader();
            readVals.Read();
            int semID = readVals.GetInt32("semId");
            readVals.Close();

            string insertSecConst = "INSERT "
                                 + "INTO SectionConsists (sectionNo, cid, semId) "
                                 + "VALUES ("+ 1 +", '"+id +"', " + semID  +")";

 

            sqlCommand = new MySqlCommand(insertStaff, conn);

            Boolean checker = false;
            try
            {
                readVals = sqlCommand.ExecuteReader();
                readVals.Close();

                sqlCommand = new MySqlCommand(insertSecConst, conn);

                readVals = sqlCommand.ExecuteReader();
                readVals.Close();

            }
            catch (MySqlException ex)
            {

                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLiteral.Text = msg;
                ErrorLiteral.Visible = true;
                checker = true;

            }

            if (!checker)
            {
                Response.Redirect("Admin_Course.aspx");
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_Course.aspx");
        }
    
    }
}